package com.gugee.tiktok.data.consumer.blogger.core.spider;

import com.gugee.tiktok.common.model.spider.market.author.MarketAuthorResponse;
import com.gugee.tiktok.common.model.spider.market.distribution.DistributionResponse;
import com.xiguaji.boot.retry.RetryableException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class MarketSpiderClient {

    @Autowired
    RestTemplate restTemplate;

    @Value("${xiguaji.spider.host}")
    String host;

    @Retryable(value = RetryableException.class, maxAttemptsExpression = "${spider.creator.marker.retry:10}")
    public MarketAuthorResponse getAuthors(int page, int records){
        String uri = host + "/CreatorMarket/AuthorSearch?page={page}&limit={records}";
        ResponseEntity<MarketAuthorResponse> responseEntity = restTemplate.getForEntity(uri, MarketAuthorResponse.class,page,records);
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new RuntimeException(String.format("get blogger error. status: %s . msg: %s", responseEntity.getStatusCodeValue(), responseEntity.getBody()));
        }

        if (responseEntity.getBody() == null) {
            throw new RetryableException("get blogger_market error, retry times 10. uri:" +
                    uri.replace("{page}", String.valueOf(page)));
        }
        return responseEntity.getBody();
    }

    @Retryable(value = RetryableException.class, maxAttemptsExpression = "${spider.creator.marker.retry:10}")
    public DistributionResponse getDistribution(long marketId){
        String uri = host + "/CreatorMarket/AudienceDistribution?userId={marketId}";
        ResponseEntity<DistributionResponse> responseEntity = restTemplate.getForEntity(uri, DistributionResponse.class,marketId);
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new RuntimeException(String.format("get blogger error. status: %s . msg: %s", responseEntity.getStatusCodeValue(), responseEntity.getBody()));
        }

        if (responseEntity.getBody() == null) {
            throw new RetryableException("get blogger distribution error, retry times 10. uri:" +
                    uri.replace("{marketId}", String.valueOf(marketId)));
        }
        return responseEntity.getBody();
    }

    @Retryable(value = RetryableException.class, maxAttemptsExpression = "${spider.creator.marker.retry:10}")
    public MarketAuthorResponse getAuthorSearchByFans(int page,int records,int minFans,int maxFans){
        String uri = host + "/CreatorMarket/AuthorSearchByFans?page={page}&limit={records}&author_min_reach={minFans}&author_max_reach={maxFans}";
        ResponseEntity<MarketAuthorResponse> responseEntity = restTemplate.getForEntity(uri, MarketAuthorResponse.class,page,records,minFans,maxFans);
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new RuntimeException(String.format("get blogger error. status: %s . msg: %s", responseEntity.getStatusCodeValue(), responseEntity.getBody()));
        }

        if (responseEntity.getBody() == null) {
            throw new RetryableException("get getAuthorSearchByFans error, retry times 10. uri:" +
                    uri.replace("{page}", String.valueOf(page)));
        }
        return responseEntity.getBody();
    }
}
